package com.rpay.web.controller.user;

import com.google.common.collect.Maps;
import com.rpay.common.dto.contract.ContractDTO;
import com.rpay.common.dto.user.UserDTO;
import com.rpay.common.dto.user.UserRippleDTO;
import com.rpay.common.enums.PcsResultCode;
import com.rpay.common.enums.SmsCodeEnum;
import com.rpay.common.enums.SmsTypeEnum;
import com.rpay.common.exception.UserException;
import com.rpay.common.util.DateUtil;
import com.rpay.common.vo.PcsResult;
import com.rpay.service.auth.IAuthService;
import com.rpay.service.config.ServiceThreadLocal;
import com.rpay.service.sms.ISmsCodeService;
import com.rpay.service.user.IUserRippleService;
import com.rpay.service.user.IUserService;
import com.rpay.service.user.IUserTradeService;
import com.rpay.web.controller.BaseController;
import com.rpay.web.interceptor.authentic.AccessControl;
import com.rpay.web.interceptor.authentic.AccessControlType;
import com.rpay.web.param.AddUserRipple;
import com.rpay.web.service.UserService;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/5/19
 * @version 1.0
 */
@RestController
@RequestMapping(value = "api/user/ripple")
@AccessControl(type = {AccessControlType.SECRET, AccessControlType.ACCESS_TOKEN})
public class UserRippleController extends BaseController{
    @Autowired
    private IUserRippleService userRippleService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private UserService userService;
    @Autowired
    private IUserTradeService iUserTradeService;
    @Autowired
    private IAuthService iAuthService;
    @Autowired
    private ISmsCodeService iSmsCodeService;

    @GetMapping(value = "contract/list")
    public PcsResult contractList() {
        List<ContractDTO> list = userRippleService.listContract();
        return new PcsResult(list);
    }

    @GetMapping(value = "list")
    public PcsResult rippleList() {
        List<UserRippleDTO> list = userRippleService.listUserRipple(ServiceThreadLocal.getUserId());
        return new PcsResult(list);
    }

    @PostMapping(value = "add")
    public PcsResult addRipple(@Validated @RequestBody AddUserRipple addUserRipple) {
        UserDTO user = getUser();
        //判断验证码
        if (SmsTypeEnum.MAIL.getCode().equals(addUserRipple.getType())) {
            iSmsCodeService.verifyMail(SmsCodeEnum.CONTRACT.getCode(), user.getEmail(), addUserRipple.getCode());
        } else {
            iAuthService.verifyAuth(user.getSecretKey(), Integer.valueOf(addUserRipple.getCode()));
        }
        UserRippleDTO userRippleDTO = new UserRippleDTO();
        userRippleDTO.setContractId(addUserRipple.getContractId());
        userRippleDTO.setInvestAmount(addUserRipple.getInvestAmount());
        //0点至2点不允许购买合约
        if (DateUtil.getHour() <= 2) {
            throw new UserException(PcsResultCode.USER_CANNOT_BUY);
        }
        UserDTO userDTO = iUserService.getUserById(ServiceThreadLocal.getUserId());
        if (userDTO.getRippleAmount().compareTo(userRippleDTO.getInvestAmount()) == -1) {
            throw new UserException(PcsResultCode.USER_RIPPLE_NOT_ENOUGH);
        }
        userRippleDTO.setUserId(userDTO.getId());
        userService.saveUserRipple(userDTO, userRippleDTO);
        return new PcsResult();
    }

    @GetMapping(value = "detail")
    public PcsResult showRipple(@RequestParam @NotNull(message = "合约id不能为空") Integer contractId, @RequestParam @NotNull(message = "购买数量不能为空") BigDecimal amount) {
        UserDTO user = getUser();
        BigDecimal price = iUserTradeService.getRate();
        Date date;
        if (contractId == 1) {
            date = DateUtil.addMonths(new Date(), 3);
        } else if (contractId == 2) {
            date = DateUtil.addMonths(new Date(), 6);
        }  else {
            date = DateUtil.addMonths(new Date(), 12);
        }
        BigDecimal totalAmount = amount.multiply(price);
        int level = user.getLevel();

        Map map = Maps.newHashMap();
        map.put("amount", amount);
        map.put("rpayAmount", totalAmount);
        map.put("date", DateUtil.formatDate(date));
        map.put("level", level);
        return newResult().setData(map);
    }
}
